package com.pxene.dmp.common;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class EncryptUtils {
	private static final char[] DATA_CV_0 = { '0', 'f', 'p' };
	private static final char[] DATA_CV_1 = { '1', 'b', 'z' };
	private static final char[] DATA_CV_2 = { '2', 's', 'a' };
	private static final char[] DATA_CV_3 = { '3', 'h', 'e' };
	private static final char[] DATA_CV_4 = { '4', 'd', 'k' };
	private static final char[] DATA_CV_5 = { '5', 'v', 'l' };
	private static final char[] DATA_CV_6 = { '6', 't', 'n' };
	private static final char[] DATA_CV_7 = { '7', 'c', 'u' };
	private static final char[] DATA_CV_8 = { '8', 'g', 'w' };
	private static final char[] DATA_CV_9 = { '9', 'y', 'j' };
    // { 'i', 'm', 'r', 'o', 'q', 'x' };
	// 符号映射表
	private static final Map<Character, Character> signs = new HashMap<Character, Character>();
	static {
		signs.put('.', 'o');
		signs.put(',', 'x');
	}
    
	private static final char[] RANDOM_CHAR = { '0', 'f', 'p' ,'1', 'b', 'z','2', 's', 'a','3', 'h', 'e','4', 'd', 'k','5', 'v', 'l','6', 't', 'n','7', 'c', 'u','8', 'g', 'w','9', 'y', 'j','i', 'm', 'r', 'o', 'q', 'x'};
    
    public static String randomString(int length) {
    	String res = "";
    	Random random = new Random();
    	for (int i=0; i<length; i++) {
    		res += RANDOM_CHAR[random.nextInt(36)];
    	}
    	return res;
    }

    // 将中文char转为unicode
    private static String chn2Unicode(char c) {
		String unicode = "";
		if (c >= 48 && c <= 57) { // 数字
			unicode = String.valueOf(c);
		} else if (c >= 97 && c <= 122) { // 字母
			unicode = String.valueOf(c);
		} else if (c >= 19968 && c <= 171941) { // 中文
			unicode = "#" + Integer.toHexString(c);
		}
		return unicode;
	}
    // 判断字符串中是否包含中卫
    public static boolean containChn(String chn) {
    	return !chn.matches("[^\u4e00-\u9fa5]+");
    }
    // 加密中文
    public static String encryptChn(String chn) {
    	if (!containChn(chn)) {
    		return chn;
    	}
    	StringBuilder sb = new StringBuilder();
    	for (int i = 0; i < chn.length(); i++) {
    		sb.append(EncryptUtils.chn2Unicode(chn.charAt(i)));
    	}
    	return sb.toString();
    }
    
    // 加密数组形式的字符串
    public static String encryptNum(String num) {
    	if (num == null) {
    		return "";
    	}
    	String res = "";
    	Random random = new Random();
        for (int i = 0; i < num.length(); i++) {
        	res += getCode(num.charAt(i), random.nextInt(3));
        }
        return res;
    }
    
    private static char getCode(char c, int index) {
        switch (c) {
        case '0':
            return DATA_CV_0[index];
        case '1':
            return DATA_CV_1[index];
        case '2':
            return DATA_CV_2[index];
        case '3':
            return DATA_CV_3[index];
        case '4':
            return DATA_CV_4[index];
        case '5':
            return DATA_CV_5[index];
        case '6':
            return DATA_CV_6[index];
        case '7':
            return DATA_CV_7[index];
        case '8':
            return DATA_CV_8[index];
        case '9':
            return DATA_CV_9[index];
        default:
    	    return signs.containsKey(c) ? signs.get(c) : ' ';
        }
    }
    
}
